package com.ddwl.goods.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.GoodsEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.common.feign.api.ShopApi;
import com.ddwl.goods.service.ItemService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.goods.*;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.goods.ItemConListVo;
import com.ddwl.schema.vo.goods.ItemDetailVo;
import com.ddwl.schema.vo.goods.ItemVo;
import com.ddwl.schema.vo.goods.SkuDtoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * @author Fu Zhaohui
 * @version 1.0.0
 * @date 2019/6/14 9:28
 */
@Api(tags = "商品服务")
@RequestMapping("item")
@RestController
@RequiredArgsConstructor
public class ItemController extends BaseController {

    private final ItemService itemService;
    private final ShopApi shopApi;

    @PostMapping("list")
    @ApiOperation("查询商品列表")
    public RestVo<PageVo<ItemVo>> list(@Valid @RequestBody PageBo<ItemSearchBo> pageBo) {
        ItemSearchBo param = Optional.ofNullable(pageBo.getParam()).orElse(new ItemSearchBo());
        if(getProduct().equals(BaseEnum.ProductEnum.FENXIAO.getName())) {
            param.setShopId(shopApi.getDefaultShopForApi().getResult().getId());
        }
        return RestVo.ok(itemService.list(pageBo));
    }

    @GetMapping("get")
    @ApiOperation("查询商品详情")
    @Auth(product = BaseEnum.ProductEnum.API)
    public RestVo<ItemVo> List(String id) {
        return RestVo.ok(itemService.getById(id));
    }

    @PostMapping("distList")
    @ApiOperation("查询商品列表附带商品分销信息")
    @Auth(product = {BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.FENXIAO})
    public RestVo<PageVo<ItemVo>> distList(@Valid @RequestBody PageBo<ItemSearchBo> pageBo) {
        if (null == pageBo.getParam()) {
            pageBo.setParam(new ItemSearchBo());
        }
        pageBo.getParam().setItemStatus(1);
        return RestVo.ok(itemService.distrList(pageBo));
    }

    @GetMapping("getTopic")
    @ApiOperation("获取商品下的专题列表")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<List<String>> getTopic(@RequestParam("itemId") String itemId) {
        return RestVo.ok(itemService.getTopic(itemId));
    }

    @PostMapping("itemSginOperate")
    @ApiOperation("商品标签操作（支持修改）")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo itemSginOperate(@RequestBody ItemSginBo itemSginBo) {
        itemService.itemSginOperate(itemSginBo);
        return RestVo.ok();
    }

    @PostMapping("addTopic")
    @ApiOperation("绑定专题")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<Integer> addTopic(@RequestBody ItemTopicNewBo itemTopicNewBo) {
        return RestVo.ok(itemService.addTopic(itemTopicNewBo.getItemId(), itemTopicNewBo.getTopicList()));
    }

    @GetMapping("delTopic")
    @ApiOperation("解除专题绑定")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<Integer> delTopic(@RequestParam("itemId") String itemId, @RequestParam("topicId") String topicId) {
        return RestVo.ok(itemService.delTopic(itemId, topicId));
    }

    @GetMapping("itemSort")
    @ApiOperation("商品排序")
    public RestVo itemSort(@RequestParam("itemId") String itemId, @RequestParam("itemSort") Integer itemSort) {
        itemService.itemSort(itemId, itemSort);
        return RestVo.ok();
    }

    @PostMapping("conList")
    @ApiOperation("管理后台商品列表（旧）")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<PageVo<ItemVo>> conList(@Valid @RequestBody PageBo<ItemSearchBo> pageBo) {
        pageBo.getParam().setItemType(GoodsEnum.CategoryTypeEnum.PLATFORM.getCode());
        return RestVo.ok(itemService.conList(pageBo));
    }

    @PostMapping("conListNew")
    @ApiOperation("管理后台商品列表(新)")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<PageVo<ItemConListVo>> conListNew(@Valid @RequestBody PageBo<ItemSearchBo> pageBo) {
        pageBo.getParam().setItemType(GoodsEnum.CategoryTypeEnum.PLATFORM.getCode());
        return RestVo.ok(itemService.conListNew(pageBo));
    }

    @PostMapping("itemCommissions")
    @ApiOperation("管理后台批量修改佣金")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo itemCommissions(@RequestBody ItemCommissionBo itemCommissionBo) {
        itemService.itemCommissions(itemCommissionBo);
        return RestVo.ok();
    }

    @PostMapping("/create")
    @ApiOperation("创建或修改商品")
//    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo<String> save(@Valid @RequestBody ItemCreateBo bo) {
        return RestVo.ok(itemService.save(bo));
    }

    @PostMapping("shelf")
    @ApiOperation("上下架商品")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo shelf(@Valid @RequestBody ItemShelfBo bo) {
        itemService.shelf(bo);
        return RestVo.ok();
    }

    @PostMapping("excelIn")
    @ApiOperation("sku-成本价Excel导入")
    public RestVo<Integer> excelIn(@RequestParam("file") MultipartFile file) throws IOException {
        return RestVo.ok(itemService.excelIn(file));
    }

    @DeleteMapping("del/{id}")
    @ApiOperation("删除商品")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo delete(@PathVariable("id") String id) {
        itemService.delete(id);
        return RestVo.ok();
    }

    @GetMapping("/detail")
    @ApiOperation("商品详情")
    @Auth(required = false, product = {BaseEnum.ProductEnum.FENXIAO, BaseEnum.ProductEnum.MALL, BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.WBM, BaseEnum.ProductEnum.WBM_OFFICE, BaseEnum.ProductEnum.BYTE_DANCE})
    public RestVo<ItemDetailVo> detail(@ApiParam(value = "商品ID", required = true) @RequestParam String id, String seckillId) {
        //加入用户模块后使用
        return RestVo.ok(itemService.detail(id, getUserId(), seckillId));
    }

    @PostMapping("/queryAll")
    @ApiOperation("商品详情")
    public RestVo<PageVo<ItemVo>> queryItemAll(@RequestBody PageBo bo) {
        return RestVo.ok(itemService.queryAllItem(bo));
    }

    @PostMapping("/querySkuWithItem")
    @ApiOperation("商品包括sku")
    @Auth(product = {BaseEnum.ProductEnum.API})
    public RestVo<List<SkuDtoVo>> querySkuWithItem(@RequestBody ItemListBo bo) {
        return RestVo.ok(itemService.querySkuWithItem(bo));
    }

    @PostMapping("/querySkuWithItemCount")
    @ApiOperation("商品包括sku数量")
    @Auth(product = {BaseEnum.ProductEnum.API})
    public RestVo<Integer> querySkuWithItemCount(@RequestBody ItemListBo bo) {
        return RestVo.ok(itemService.querySkuWithItemCount(bo));
    }

    @PostMapping("/queryItems")
    @ApiOperation("查询商品")
    @Auth(product = {BaseEnum.ProductEnum.API})
    public RestVo<List<ItemVo>> queryItems(@RequestBody Collection<String> itemIds) {
        return RestVo.ok(itemService.queryItems(itemIds));
    }

    @PostMapping("/sendZeroCountMessage")
    @ApiOperation("商品包括sku数量")
    public RestVo sendZeroCountMessage() {
        ItemListBo bo = new ItemListBo();
        bo.setMaxStock(0);
        bo.setItemStatus(1);
        itemService.sendZeroCountMessage(bo);
        return RestVo.ok();
    }

    @PostMapping("/generateWxaCode")
    @ApiOperation(value = "生成商品二维码", hidden = true)
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    public RestVo generateWxaCode() {
        itemService.generateWxaCode();
        return RestVo.ok();
    }

    @PostMapping("/wxaCode/down")
    @ApiOperation(value = "下载商品小程序码")
    @Auth(product = {BaseEnum.ProductEnum.CONSOLE, BaseEnum.ProductEnum.SHOP_CONSOLE})
    public void downItemWxaCodeForZip(HttpServletResponse response, @RequestBody List<String> itemIds) {
        itemService.downWxaCode(response, itemIds, null);
    }

}
